﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iLangl.Model;
using iLangl.BusinessModel.DAL;
using iLangl.Security;

namespace iLangl.BusinessModel.Managers
{
	public class CompanyManager
	{
		#region Singelton Pattern
		private CompanyManager()
		{
		}

		private static CompanyManager instance;
		private static object mutex = new object();
		/// <summary>
		/// The instance of the CompanyManager.
		/// </summary>
		public static CompanyManager Instance
		{
			get
			{
				if (instance == null)
				{
					lock (mutex)
					{
						if (instance == null)
						{
							instance = new CompanyManager();
						}
					}
				}
				return instance;
			}
		}
		#endregion

		public void UpdateCompanyInfo(Company company)
		{
			//Add Contacts
			if (company.Contacts != null)
				foreach (Contact contact in company.Contacts)
				{
					ContactManager.Instance.UpdateContact(contact);
				}

			DALFacade.Instance.CompanyDAL.UpdateCompany(company, null);
		}

		public bool FindCompany(Company company)
		{
			throw new NotImplementedException();
		}

		public Company GetCompany(String companyId)
		{
			throw new NotImplementedException();
		}

		public void FilCompany(Company company)
		{
			throw new NotImplementedException();
		}

		public void CreateCompany(Company company)
		{
			DALFacade.Instance.CompanyDAL.CreateCompany(company, null);
		}

		public void CreateCompany(Company company, String primaryContactId, String userId)
		{
			Contact primarycontact = null;

			//Add Contacts
			foreach (Contact contact in company.Contacts)
			{
				if (contact.Id == primaryContactId)
				{
					primarycontact = contact;
				}

				ContactManager.Instance.CreateContact(contact);
			}


			if (primarycontact != null)
			{
				//TODO: Change in future to normal behaiviour when Authentication Service will be ready
				AuthenticationManager.Instance.AssignContactIdToUserAccountId(userId, primarycontact.Id);
			}

			DALFacade.Instance.CompanyDAL.CreateCompany(company, null);
		}

		public void AddSubscriber(Company company, PluginLicense license, bool isNeedtoSync)
		{
			//We not add subscribers for plugin where push service is not available
			if (!license.Plugin.IsPushingEnabled)
			{
				return;
			}

			SynchronizationManager.Instance.AddSubscriber(eEntityType.Company, company.Id, license, isNeedtoSync);

			if (company.BillingInfo != null)
				SynchronizationManager.Instance.AddSubscriber(eEntityType.BillingInfo, company.BillingInfo.Id, license, isNeedtoSync);

			//Add Contacts
			if (company.Contacts != null)
				foreach (Contact contact in company.Contacts)
				{
					SynchronizationManager.Instance.AddSubscriber(eEntityType.Contact, contact.Id, license, isNeedtoSync);
				}
		}

		public void RemoveSubscriber(Company company, String subscriberId, bool isNeedtoSync)
		{
			SynchronizationManager.Instance.RemoveSubscriber(eEntityType.Company, company.Id, subscriberId);

			if (company.BillingInfo != null)
				SynchronizationManager.Instance.RemoveSubscriber(eEntityType.BillingInfo, company.BillingInfo.Id, subscriberId);

			//Add Contacts
			if (company.Contacts != null)
				foreach (Contact contact in company.Contacts)
				{
					SynchronizationManager.Instance.RemoveSubscriber(eEntityType.Contact, contact.Id, subscriberId);
				}
		}

		public void UpdateCompany(Company company)
		{
			if (company == null)
				return;

			//Add Contacts
			if (company.Contacts != null)
				foreach (Contact contact in company.Contacts)
				{
					ContactManager.Instance.UpdateContact(contact);
				}

			DALFacade.Instance.CompanyDAL.UpdateCompany(company, null);
		}
	}
}